﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using KaoQin.BLL;
using KaoQin.Model;
using KaoQin.Common;

namespace KaoQin.WPFApp.sys.User
{
    /// <summary>
    /// SysUserMgr.xaml 的交互逻辑
    /// </summary>
    public partial class SysUserMgr : Window
    {
        public SysUserMgr()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面加载事件
        /// yd 2012-10-29
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            dataBind();
        }

        /// <summary>
        /// 绑定列表
        /// yd 2012-10-29
        /// </summary>
        protected void dataBind()
        {
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(txtKey.Text.Trim()))
            {
                sql = "RealName like '%" + txtKey.Text.Trim() + "%'";
            }

            lstUser.ItemsSource = new SysUsers().GetModelList(sql);
            
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            dataBind();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SysUsersInfo model = (SysUsersInfo)gridInfo.DataContext;
                if (model == null)
                {
                    MessageBox.Show("请选择要修改的用户");
                    return;
                }
                if (string.IsNullOrEmpty(model.KName))
                {
                    MessageBox.Show("登录帐号不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(model.Password))
                {
                    MessageBox.Show("登录密码不能为空");
                    return;
                }
                model.ID = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;

                


                new SysUsers().Add(model);
                MessageBox.Show("新增成功");
                dataBind();
            }
            catch (Exception ex)
            {
                Logger.Write("新增失败：" + ex.Message);
                MessageBox.Show("新增失败:" + ex.Message);
            }
            
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SysUsersInfo model = (SysUsersInfo)gridInfo.DataContext;
                if (model == null)
                {
                    MessageBox.Show("请选择要修改的用户");
                    return;
                }
                if (string.IsNullOrEmpty(model.KName))
                {
                    MessageBox.Show("登录帐号不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(model.Password))
                {
                    MessageBox.Show("登录密码不能为空");
                    return;
                }
                new SysUsers().Update(model);
                MessageBox.Show("修改成功");
                dataBind();
            }
            catch (Exception ex)
            {
                Logger.Write("修改失败：" + ex.Message);
                MessageBox.Show("修改失败:" + ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定删除吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    SysUsersInfo model = (SysUsersInfo)gridInfo.DataContext;
                    if (model == null)
                    {
                        MessageBox.Show("请选择要删除的用户");
                        return;
                    }

                    new SysUsers().Delete(model.ID);
                    dataBind();
                    MessageBox.Show("删除成功");
                }
            }
            catch (Exception ex)
            {
                 Logger.Write("删除失败：" + ex.Message);
                MessageBox.Show("删除失败:" + ex.Message);
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


    }
}
